
#include <QTextStream>
#include <QTextCodec>
#include "userprofile.h"

UserProfile::UserProfile(Profile *parent):
    Profile(parent)
{
    parameters_size=6;
    settings_key="BalanceLastFile";
}

UserProfile::UserProfile(QString &file_name, Profile *parent):
    Profile(parent)
{
    parameters_size=6;
    m_file_name=file_name;
    settings_key="BalanceLastFile";
    if(!QFile::exists(m_file_name))
    {
        QFile file(m_file_name);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
            return ;
        file.close();
    }
}

UserProfile::UserProfile(QString file_name, QStringList &params, unsigned char *key, ServiceProvider *serviceProvider, Profile *parent):
    Profile(parent),
    m_serviceProvider(serviceProvider)
{
    parameters_size=6;
    m_file_name=file_name;
    settings_key="BalanceLastFile";
    if(!QFile::exists(m_file_name))
    {
        QFile file(m_file_name);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
            return ;
        file.close();
    }
    SetParameters(params, key);
}

UserProfile::~UserProfile()
{

}


QStringList &UserProfile::GetParamList()
{
    data_list.clear();
    data_list<< m_user_name
             <<m_user_login
             <<m_password
             <<m_provider_name
             <<m_file_name
             <<m_balance
             <<QString::number(m_login_type);
    return data_list;
}

void UserProfile::SaveParamsFromList(QStringList &params)
{
    data_list.clear();
    data_list<<params;
    if(params.size()>parameters_size-1)
    {
        m_user_name=params.at(0);
        m_user_login=params.at(1);
        m_password=params.at(2);
        m_provider_name=params.at(3);
        m_balance=params.at(4);
        m_login_type=params.at(5).toInt();
    }
    else if(params.size()>3) //old profile variant
    {
        m_user_name=params.at(0);
        m_user_login=params.at(1);
        m_password=params.at(2);
        m_provider_name=params.at(3);
    }
}

//getters
QString         UserProfile::user_name()
{
    return m_user_name;
}

QString         UserProfile::user_login()
{
    return m_user_login;
}

QString         UserProfile::password()
{
    return m_password;
}

QString         UserProfile::file_name()
{
    return m_file_name;
}

QString         UserProfile::provider_name()
{
    return m_provider_name;
}

QString         UserProfile::balance()
{
    return m_balance;
}

const ServiceProvider& UserProfile::serviceProvider()
{
    return (const ServiceProvider&)*m_serviceProvider;
}

int             UserProfile::login_type()
{
    return m_login_type;
}

//setters
void            UserProfile::set_user_name(QString value)
{
    m_user_name=value;
}

void            UserProfile::set_user_login(QString value)
{
    m_user_login=value;
}

void            UserProfile::set_password(QString value)
{
    m_password=value;
}

void            UserProfile::set_provider_name(QString value)
{
    m_provider_name=value;
}

void            UserProfile::set_file_name(QString value)
{
    m_file_name=value;
}

void            UserProfile::set_balance(QString value)
{
    m_balance=value;
}

void            UserProfile::set_service_provider(ServiceProvider* serviceProvider)
{
    m_serviceProvider=serviceProvider;
}

void            UserProfile::set_login_type(int type)
{
    m_login_type=type;
}

QString* UserProfile::StringFromParameters()
{
    QString *str=new QString(
                     m_user_name+"||"
                     +m_user_login+"||"
                     +m_password+"||"
                     +m_provider_name+"||"
                     +m_balance+"||"
                     +QString::number(m_login_type));
    return str;
}

bool  UserProfile::operator==(UserProfile & other) const
{
    if((!(m_user_name.compare(other.user_name())))&&
            (!(m_user_login.compare(other.user_login())))&&
            (!(m_password.compare(other.password())))&&
            (!(m_provider_name.compare(other.password())))&&
            (!(m_file_name.compare(other.file_name())))&&
            (m_login_type==other.login_type())
            )
        return true;
    else
        return false;
}
UserProfile     &UserProfile::operator=(UserProfile & other)
{
    m_user_name=other.user_name();
    m_user_login=other.user_login();
    m_password=other.password();
    m_provider_name=other.provider_name();
    m_file_name=other.file_name();
    m_login_type=other.login_type();
    return *this;
}
